import { ref, onMounted } from 'vue'
import { getCategoryById } from '@/api/category.js'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'
export function useCategory() {
  // 二级分类数据
  const route = useRoute()
  const categoryData = ref({})
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryById(id)
    categoryData.value = res.result
  }

  onMounted(() => getCategory())

  // 路由跳转时执行
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id)
  })
  return { categoryData }
}
